<%--
  -	$Version: 0.1 $
  -	$Date: 2008-09-09 16:48:22 $
--%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%@ page import="org.jawa.Constants,org.jawa.util.ParamUtils
	,org.jawa.util.ClassUtils
	,org.jawa.database.*
	,java.io.File
	,java.sql.*
	,java.util.HashMap
	,java.util.Locale
	,java.util.Map
	,java.util.StringTokenizer"
%>

<%! // Global vars
	static final String STANDARD = "standard";
	static final String EMBEDDED = "embedded";
	  boolean testConnection(Map<String,String> errors) {
	        boolean success = true;
	        Connection con = null;
	        try {
	            con = DbConnectionManager.getConnection();
	            if (con == null) {
	                success = false;
	                errors.put("general","A connection to the database could not be "
	                    + "made. View the error message by opening the "
	                    + "\"" + File.separator + "logs" + File.separator + "error.log\" log "
	                    + "file, then go back to fix the problem.");
	            }
	            else {
	            	// See if the Jive db schema is installed.
	            	try {
	            		Statement stmt = con.createStatement();
	            		// Pick an arbitrary table to see if it's there.
	            		stmt.executeQuery("SELECT 1 FROM app_user");
	            		stmt.close();
	            	}
	            	catch (SQLException sqle) {
	                    success = false;
	                    errors.put("general","The Openfire database schema does not "
	                        + "appear to be installed. Follow the installation guide to "
	                        + "fix this error.");
	            	}
	            }
	        }
	        catch (Exception ignored) {}
	        finally {
	            try {
	        	    con.close();
	            } catch (Exception ignored) {}
	        }
	        return success;
	    }
%>
<%
    // Get parameters
    String mode = ParamUtils.getParameter(request,"mode");
    boolean next = ParamUtils.getBooleanParameter(request,"next");

    // handle a mode redirect
    Map<String,String> errors = new HashMap<String,String>();
    if (next) {
        if (STANDARD.equals(mode)) {
            response.sendRedirect("setup-datasource-standard-select.jsp");
            return;
        }
        else if (EMBEDDED.equals(mode)) {
            // Set the classname of the provider in the config file:
            Constants.setXMLProperty("connectionProvider.className",
                    "org.jawa.database.EmbeddedConnectionProvider");
            Constants.setXMLProperty("hibernate.dialect",
                    "org.hibernate.dialect.HSQLDialect");
       
            //if (testConnection(errors)) //内置的数据库，不用测试
            {
                // Redirect
            	 response.sendRedirect("setup-mail-settings.jsp");
                 return; 
            }
        }
    }

    // Defaults
    if (mode == null) {
        // If the "embedded-database" directory exists, select to the embedded db as the default.
        if (new java.io.File(Constants.getHomeDirectory(), "conf/database").exists()) {
            mode = EMBEDDED;
        }
        // Otherwise default to standard.
        else {
            mode = STANDARD;
        }
    }
%>

<html>
<head>
<title><fmt:message key="setup.datasource.setting.title" /></title>
<meta name="currentStep" content="3" />
</head>
<body>
<h1><fmt:message key="setup.datasource.setting.title" /></h1>
<p><fmt:message key="setup.datasource.settings.info" /></p>

<div class="jawa-contentBox">
	<form action="setup-datasource-settings.jsp">
		<input type="hidden" name="next" value="true">
		<table cellpadding="3" cellspacing="2" border="0">
			<tr>
				<td align="center" valign="top">
					<input type="radio" name="mode" value="<%= STANDARD %>" id="rb02"
         				<%= ((STANDARD.equals(mode)) ? "checked" : "") %>>
				</td>
				<td>
					<label for="rb02">
						<b>
							<fmt:message key="setup.datasource.settings.connect" />
						</b>
					</label>
					<br>
					<fmt:message key="setup.datasource.settings.connect_info" />
				</td>
			</tr>
			
			<tr>
				<td align="center" valign="top">
					<input type="radio" name="mode" value="<%= EMBEDDED %>" id="rb01"
         				<%= ((EMBEDDED.equals(mode)) ? "checked" : "") %>>
				</td>
				<td>
					<label for="rb01"><b>
						<fmt:message key="setup.datasource.settings.embedded" /></b>
					</label>
					<br>
					<fmt:message key="setup.datasource.settings.embedded_info" />
				</td>
			</tr>
		</table>
		<br>
		<br>
		<div align="right">
			<input type="Submit" name="continue" 
				value="<fmt:message key="button.continue" />" id="jawa-setup-save"	border="0">
		</div>
	</form>
</div>
</body>
</html>
